package ajax.mantainx.controller;

import ajax.mantainx.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author starrysky
 * @title: AjaxController
 * @projectName springMvc_Parent
 * @description:  控制层
 * @date 2021/2/614:48
 */
@RestController
public class AjaxController {
    @GetMapping("/hello")
    public String hello(){
        return "Hello~AJAX!";
    }
    @PostMapping("/getInfo")
    public void getInfo(String name, HttpServletResponse response) throws IOException {
        System.out.println("param"+"====>"+name);
        if ("keys".equals(name)){
            response.getWriter().println("true");
        }else {
            response.getWriter().println("false");
        }
    }

    @RequestMapping (value = "/a1",produces = "application/json;charset=utf-8")
    public List<User> getData(){
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"虚实1",16L,"201920201590046"));
        userList.add(new User(2,"虚实2",16L,"201920201590047"));
        userList.add(new User(3,"虚实3",16L,"201920201590048"));
        return userList;
    }

    @RequestMapping("/a2")
    public String login(String username,String password){
        String temp  = "";
        if (username!=null){
            if ("admin".equals(username)){
                temp = "ok";
            }else {
                temp = "用户名错误";
            }
        }

        if (password!=null){
            if ("123".equals(password)){
                temp = "ok";
            }else{
                temp  = "密码错误";
            }
        }
        return temp;
    }
}
